
var app = getApp();

Page({

  data: {

    // 1.菜单栏数据
    menuItems: [
      {
        courseName: '挂号加号',
        id: '0'
      },
      {
        courseName: 'B超预约',
        id: '1'
      },
      {
        courseName: '闲时预约',
        id: '2'
      }

    ],

    items: [],
    vip: 'N',


    // 2.列表
    lists: [0, 1, 2],

    // 3.item数据
    image: "../../image/0713head.png",
    interval: 3000,
    duration: 1200,
    color: 'light-gray',
    currentMenuID: '0',
    currentPage: 0,

    hideHeader: true,
    hideBottom: true,
    refreshTime: '', // 刷新的时间 
    contentlist: [], // 列表显示的数据源
    allPages: '',    // 总页数
    detailcurrentPage: 1,  // 当前页数  默认是1
    loadMoreData: '加载更多……'
  },


  // 3.点击菜单栏
  tapMenuItem: function (e) {

    let id = e.currentTarget.id
    this.setData({
      currentMenuID: id,
      currentPage: id
    })
  },

  // 滑动改变当前页面
  changeCurrentPage: function (e) {

    var that = this;

    let id = e.detail.current
    this.setData({
      currentMenuID: id,
      currentPage: id
    });

    that.init(e);

  },

  bookAddition: function (e) {
    var that = this;

    var index = e.currentTarget.dataset.index;
    var item = that.data.items[index];

    wx.navigateTo({
      url: '/pages/booking/addition/index?TID=' + item.TID
    })
  },

  bookBC: function (e) {
    var that = this;

    var index = e.currentTarget.dataset.index;
    var item = that.data.items[index];

    wx.navigateTo({
      url: '/pages/booking/bc/index?TID=' + item.TID
    })
  },


  bookFree: function (e) {
    var that = this;

    var index = e.currentTarget.dataset.index;
    var item = that.data.items[index];

    wx.navigateTo({
      url: '/pages/booking/free/index?TID=' + item.TID
    })
  },

  loadData: function (url) {

    var that = this;
    app.userlogin();
    var atkey = wx.getStorageSync("AccessToken");
    var User = wx.getStorageSync("User");

    wx.showLoading({
      title: '数据加载中...'
    })

    // --------- 发送凭证 ------------------
    wx.request({
      url: url,
      data: { AccessToken: atkey },
      header: {
        'content-type': 'application/json'
      },

      success: function (res) {

        var list = [];
        for (var i = 0; i < res.data.datum.length; i++) {
          list.push(res.data.datum[i]);
        }

        that.setData({
          items: list
        });

        wx.hideLoading();

      }
    })
  },

  init: function (options) {
    this.getData();
  },

  getData: function () {

    var that = this;

    if (that.data.currentMenuID == '0') {
      that.loadData(app.globalData.getAdditionList);
    } else if (that.data.currentMenuID == '1') {
      that.loadData(app.globalData.getBCBookingList);
    } else {
      that.loadData(app.globalData.getFreeBookingList);
    }

  },

  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    var that = this;

    that.init(options);

  },


  // 上拉加载更多
  loadMore: function () {
    var self = this;
    // 当前页是最后一页
    if (self.data.detailcurrentPage == self.data.allPages) {
      self.setData({
        loadMoreData: '已经到顶'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      var tempCurrentPage = self.data.detailcurrentPage;
      tempCurrentPage = tempCurrentPage + 1;
      self.setData({
        detailcurrentPage: tempCurrentPage,
        hideBottom: false
      })
      self.getData();
    }, 300);
  },

  // 下拉刷新
  refresh: function (e) {
    var self = this;
    setTimeout(function () {
      console.log('下拉刷新');
      var date = new Date();
      self.setData({
        detailcurrentPage: 1,
        refreshTime: date.toLocaleTimeString(),
        hideHeader: false
      })
      self.getData();
    }, 300);
  },


})



